<?php
function get_pages_with_child_count() {
	global $wpdb;

	$query = "SELECT p1.ID, p1.post_title, menu_order, p1.post_parent, (SELECT count(p2.post_parent) FROM $wpdb->posts p2 WHERE p2.post_parent = p1.ID) as child_count FROM $wpdb->posts p1 where p1.post_type = 'page' and post_status = 'publish' order by post_parent ASC, menu_order ASC";
	$pages = $wpdb->get_results($query);
	return $pages;
	
}
?>

<!-- Menu bar -->
<div class="menuBar">
	<a class="menuButton" href="/blog">Accueil</a>
	<a class="menuButton" href="/blog/?cat=221">Culture</a>
	<?php 
		$pages = get_pages_with_child_count();
		$parentPages = array();
		foreach ( $pages as $page ) {
		  if ($page->post_parent) {
			$parentPages[$page->post_parent] = 'true';
		  } else {
			echo '<a class="menuButton" href="/blog/?page_id='.$page->ID.'"';
			if ($page->child_count != 0) {
			   echo ' onmouseover="buttonMouseover(event, \'menu'.$page->ID.'\');"';
			}
			echo '>'.$page->post_title.'</a>';
		  }
		}
	?>
</div>

<!-- Sub menus. -->
<?php
	foreach ( $parentPages as $parentPage => $activated) {
?>
  <div id="menu<?php echo $parentPage ?>" class="menu" onmouseover="menuMouseover(event)">
	 <?php 
		foreach ( $pages as $page ) {
			if ($page->post_parent == $parentPage) {
				echo '<a class="menuItem" href="/blog/?page_id='.$page->ID.'">'.$page->post_title.'</a>';
			}
		}
	?>
  </div>
<?php
        }
?>